//
//  BindingAliPayTableViewController.m
//  EasyToTesco2.0
//
//  Created by knew on 2017/3/20.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "BindingAliPayTableViewController.h"
#import "BindingBankCardTableViewCell.h"
#import "LogInPassWordFooterView.h"

@interface BindingAliPayTableViewController ()<LogInPassWordFooterDelegate>

@property (nonatomic,strong)NSMutableArray *textFieldArray;

@property (nonatomic,strong)NSArray *leftArray;

@property (nonatomic,strong)NSArray *rightArray;

@end

@implementation BindingAliPayTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"绑定支付宝";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tableView.backgroundColor = BACKGROUND_COLOR;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return 0.01;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    BindingBankCardTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AliPay"];
    
    if (!cell) {
        cell = [[BindingBankCardTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AliPay"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    cell.leftLabel.text = self.leftArray[indexPath.row];
    cell.rightTextField.placeholder = self.rightArray[indexPath.row];
    cell.rightTextField.tag = indexPath.row;
    [self.textFieldArray addObject:cell.rightTextField];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return HEIGHT/5;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    LogInPassWordFooterView *footer = [[LogInPassWordFooterView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/5)];
    footer.delegate = self;
    return footer;
}

- (void)LogInPassWordFooterViewButton:(UIButton *)button {
    [self clearResignFirstResponder];
    
    UITextField *bankNumber = self.textFieldArray.firstObject;
    
    UITextField *phoneNumber = self.textFieldArray[1];
    
    UITextField *realPhoneNumber = self.textFieldArray.lastObject;
    
    if ((bankNumber.text.length == 0) || (phoneNumber.text.length == 0) ||(realPhoneNumber.text.length == 0)) {
        
        MBProgressHUD *hud = [MBProgressHUD showMessage:@"请确保信息输入无误" toView:self.view dimBackground:NO AndCompleteBlock:nil];
        [self.view addSubview:hud];
        return;
    }
    // username， alipay_realname，alipay_account，re_alipay_account
    NSDictionary *params = @{
                             @"username":USERNAME,
                             @"alipay_realname":bankNumber.text,
                             @"alipay_account":phoneNumber.text,
                             @"re_alipay_account":realPhoneNumber.text
                             };
    
    NSLog(@"%@",params);
    
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/Alipay/add" completeBlock:^(id object) {
        
        [JudgeBackDataCodeMessage JudgeBackDataCodeMessage:@"绑定支付宝账号成功" andObjece:object andAddHUDView:self.view andPopViewController:^(id object) {
            [self.navigationController popViewControllerAnimated:YES];
        }];
        
    }];
}

- (void)didViewClickClearTextField {
    [self clearResignFirstResponder];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self clearResignFirstResponder];
}

- (void)clearResignFirstResponder {
    for (UITextField *textField in self.textFieldArray) {
        [textField resignFirstResponder];
    }
}


- (NSMutableArray *)textFieldArray {
    if (!_textFieldArray) {
        _textFieldArray = [NSMutableArray array];
    }
    return _textFieldArray;
}

-(NSArray *)leftArray {
    if (!_leftArray) {
        
        NSString *path = [[NSBundle mainBundle]pathForResource:@"BindingAliPayLeft" ofType:@"plist"];
        _leftArray = [NSArray arrayWithContentsOfFile:path];
        
    }
    return _leftArray;
}

- (NSArray *)rightArray {
    if (!_rightArray) {
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"BindingAliPayRight" ofType:@"plist"];
        _rightArray = [NSArray arrayWithContentsOfFile:path];
    }
    return _rightArray;
}

@end
